/*  ----------------------------------------------------------------------------
 *  Arello Mobile
 *  Fast 3d Engine
 *  ----------------------------------------------------------------------------
 * Except where otherwise noted, this work is licensed under
 * http://creativecommons.org/licenses/by/3.0
 */

#include "SoundEnviroment.h"
#include <sound/Manager.h>
#include <sound/mixer.h>
#include <sound/SoundListener3D.h>

namespace liba
{
namespace lib3d
{
namespace node
{

const Atom SoundEnviroment::node_name( "SoundEnviroment" );

SoundEnviroment::SoundEnviroment( const Atom & name, Node * parent ):
	Node( name, parent ),
	preset("GENERIC"),
	radius(0),
	StaticContainer<SoundEnviroment>(this)
{
}

bool SoundEnviroment::node_in_enviroment(const Node * node )
{
	Vector pos = node->get_full_matrix().shift();
	Vector node_pos = node->get_full_matrix().shift();
	return (node_pos - pos).length_2() < radius * radius;
}

void SoundEnviroment::select()
{
	sound::Manager::mixer()->get_listener()->set_enviroment( preset );
}

bool SoundEnviroment::select_enviroment( const Node * node )
{
	StaticContainer<SoundEnviroment>::iterator i = StaticContainer<SoundEnviroment>::begin();
	for(; i!= StaticContainer<SoundEnviroment>::end(); ++i)
	{
		if( i->node_in_enviroment( node ) )
		{
			i->select();
			return true;
		}
	}
	return false;
}

bool SoundEnviroment::on_attribute( xml::Provider * prov, const std::string & name, const std::string & value )
{
	if( name == "Preset" )
		return converter::convert( value, &preset );
	if( name == "Radius" )
		return converter::convert( value, &radius );
	return Node::on_attribute( prov, name, value );
}

} // namespace node
} // namespace lib3d
} // liba


